﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Mbs.Modularity.AddIns;
using Mbs.Collections;
using System.Windows.Forms;

namespace Mbs.Test.ElementBuilder
{
    [TestFixture]
    public class AddInTreeTest:TestBase
    {
        IAddInTree tree;
        protected override void Init()
        {
            ServiceRegistry.Current.Register<IAddInTree, AddInTree>();
            tree = ServiceLocator.Current.Get<IAddInTree>();
        }

        private void InstallAddIn(string addInFile)
        {
            tree.Install(AppDomain.CurrentDomain.BaseDirectory + addInFile);
        }

        [Test]
        public void Test()
        {

            InstallAddIn(@"\Data\AddIn\Mbs.AddIn");
            InstallAddIn(@"\Data\AddIn\Shell.Moule.dll.AddIn");
            InstallAddIn(@"\Data\AddIn\ModuleManager.Module.dll.AddIn");
            InstallAddIn(@"\Data\AddIn\ElementMap.Module.dll.AddIn");
            InstallAddIn(@"\Data\AddIn\EventBroker.Module.dll.AddIn");
            InstallAddIn(@"\Data\AddIn\MessageBus.Threads.Module.dll.AddIn");

            Assert.IsTrue(tree.AddIns.Count() > 1);

            tree.AddIns.Foreach(addin=>addin.Start());

            var mainMenu = ServiceLocator.Current.Get<MenuStrip>("Shell/MainMenu");
            Assert.IsNotNull(mainMenu);

            var toolStrip = ServiceLocator.Current.Get<ToolStrip>("Shell/ToolBar");
            Assert.IsNotNull(toolStrip);

            Print(mainMenu);
            Print(toolStrip);
            Assert.IsTrue(mainMenu.Items.Count >= 1);
            
        }

        void Print(ToolStrip strip)
        {
            strip.Items.Cast<ToolStripItem>().Foreach(item => Print(item));
        }

        private void Print(ToolStripItem item)
        {
            Console.WriteLine(item.Name);

            var dropdown = item as ToolStripDropDownItem;
            if (dropdown != null && dropdown.DropDownItems.Count > 0)
                dropdown.DropDownItems.Cast<ToolStripItem>().Foreach(p => Print(p));
        }
    }
}
